import psutil
import time

def print_io_rate(pid):
    process = psutil.Process(pid)
    
    # 初始读写字节数
    prev_read_bytes = process.io_counters().read_bytes
    prev_write_bytes = process.io_counters().write_bytes
    
    while True:
        time.sleep(1)  # 等待1秒
        
        # 当前读写字节数
        current_read_bytes = process.io_counters().read_bytes
        current_write_bytes = process.io_counters().write_bytes
        
        # 计算读写速率
        read_rate = (current_read_bytes - prev_read_bytes) / 1024  # 转换为KB
        write_rate = (current_write_bytes - prev_write_bytes) / 1024  # 转换为KB
        
        print(f"PID {pid} - Read: {read_rate:.2f} KB/s, Write: {write_rate:.2f} KB/s")
        
        # 更新为当前值以便下一次计算
        prev_read_bytes = current_read_bytes
        prev_write_bytes = current_write_bytes

if __name__ == "__main__":
    pid = int(input("请输入要监控的进程PID: "))
    print_io_rate(pid)